package leetcode.stack_queue;

public class MyCircularDeque {

    private int[] element;
    private int capacity;
    private int rear, front;


    public MyCircularDeque(int k) {
        element = new int[k + 1];
        capacity = k + 1;
        rear = front = 0;
    }

    public boolean insertFront(int value) {
        if (isFull()) return false;
        front = (front - 1 + capacity) % capacity;
        element[front] = value;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()) return false;
        element[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()) return false;
        front = (front + 1) % capacity;
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()) return false;
        rear = (rear - 1 + capacity) % capacity;
        return true;
    }

    public int getFront() {
        if (isEmpty()) return -1;
        return element[front];
    }

    public int getRear() {
        if (isEmpty()) return -1;
        return element[(rear - 1 + capacity) % capacity];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return (front - 1 + capacity) % capacity == rear;
    }








}
